/*
 * Copyright 2011 group coding kata nantes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.coding.kata.nantes.roman.michel;

import fr.coding.kata.nantes.roman.commons.RomanToNumeralsInterface;
import org.apache.commons.lang.NotImplementedException;

class RomanToNumerals implements RomanToNumeralsInterface {
	static final String I = "I" 
	String romans
	static Map romansToNumber = [
		"I":  1,
		"V":  5,
		"X":  10,
		"L":  50,
		"C":  100,
		"D":  500,
		"M":  1000
	]

	RomanToNumeralsInterface with(String romans) {
		this.romans = romans
		this
	}

	Integer call() {
		def number = 0
		def romansLength = romans.length()
		for (int i = 0; i < romansLength; ++i) {
			def letter = romans[i]
			if (I == letter && i < romansLength -1 && I != romans[i + 1] ) number -= 1
			else number += romansToNumber[letter]
		}
		number
	}
}
